<template>
    <el-button @click="handleClick" type="primary">
      <slot/>
    </el-button>
    <div class="m-choose-icon-dialog-body-height">
      <el-dialog :title="props.title" v-model="dialogVisible">
        <div class="container">
          <div
              class="item"
              v-for="(item,index) in Object.keys(ElIcons)"
              :key="index"
              @click="clickItem(item)"
          >
            <div>
              <component :is='`el-icon-${toLine(item)}`'></component>
            </div>
            <div>{{item}}</div>
          </div>
        </div>
      </el-dialog>
    </div>
</template>

<script setup lang="ts">
import * as ElIcons from '@element-plus/icons'
import {ref,watch} from 'vue'
import {toLine} from "../../../utils";
import {useCopy} from "../../../hooks/useCopy";
let props = defineProps<{
  title:string,
  visible:boolean
}>()
let dialogVisible = ref<boolean>(props.visible)
let emit = defineEmits(['update:visible'])
const handleClick = ()=>{
  emit('update:visible', !props.visible)
}

const clickItem = (item:ElIcons)=>{
  let text = `<el-icon-${toLine(item)} />`
  useCopy(text)
  dialogVisible.value = false
}


watch(()=>props.visible, (value)=>{
  dialogVisible.value = value
})

// 监听组件内部的dialogVisible变化
watch(() => dialogVisible.value, val => {
  emit('update:visible', val)
})
</script>

<style scoped lang="scss">
.container{
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.item{
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
}
</style>
